package com.sample.javabean;
/**
 * 用户管理类
 * @author 深深
 * @version 1.0
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *  定义用户模型，封装用户属性及方法
 * @version 1.0
 */
import com.sample.action.form.UserForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;


public class User {
	private String user_id;//工号
	private String role_name;//角色名
	private String full_name;//姓名
	private String pwd;//密码
	private String ins_name;//机构名
	private final static Log log=LogFactory.getLog(User.class);

		private JdbcUtils jdbcUtils = new JdbcUtils();

		
	public User(String user_id, String role_name, String full_name, String pwd, String ins_name) {
		super();
		this.user_id = user_id;
		this.role_name = role_name;
		this.full_name = full_name;
		this.pwd = pwd;
		this.ins_name = ins_name;
	}
	public User() {
		super();
	}
	
/**
 * 加载用户信息
 * @throws Exception
 */
	public void load()throws Exception{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null; 
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "select * from  t_user where user_id='"+this.user_id+"'";
			// 4:执行SQL并返回结果
			rs=stmt.executeQuery(sql);
			if(rs.next()){
				this.user_id =rs.getString("user_id");
				this.pwd =rs.getString("pwd");
				this.full_name =rs.getString("full_name");
				this.role_name=rs.getString("role_name");
				this.ins_name=rs.getString("ins_name");
			}
		
		} catch (SQLException e) {
			System.out.println("user_id = "+this.user_id+",role_name = "+this.role_name
					+",full_name"+this.full_name+",pwd = "+this.pwd+",ins_name = "+this.ins_name);
			throw new Exception("load" + e.getCause().getClass() + "," + e.getCause().getMessage());
		} finally {
			jdbcUtils.close(conn, stmt);
		}	
	}
	/**
	 * 添加用户
	 */
	public void addUser(UserForm form)throws Exception {

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "insert into t_user(role_name,user_id,full_name,pwd,ins_name) values('"
					    + form.getRole_name() + "','" + form.getUser_id() + "','" + form.getFull_name()
					    + "','"+form.getPwd()+"','"+form.getIns_name()+"')";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			System.out.println("user_id = "+form.getUser_id()+",role_name = "+form.getRole_name()
					+",full_name"+form.getFull_name()+",pwd = "+form.getPwd()+",ins_name = "+form.getIns_name());
			throw new Exception("add" + e.getCause().getClass() + "," + e.getCause().getMessage());
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}
	/**
	 * 修改用户
	 */
	public void editUser(UserForm form)throws Exception{

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "update  t_user set full_name='"+form.getFull_name()+"',pwd='"+form.getPwd()
					   +"',role_name='"+form.getRole_name()+"',ins_name = '"+form.getIns_name()+"' where user_id='"+form.getUser_id()+"'";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			System.out.println("user_id = "+form.getUser_id()+",role_name = "+form.getRole_name()
					+",full_name"+form.getFull_name()+",pwd = "+form.getPwd()+",ins_name = "+form.getIns_name());
			throw new Exception("Exception_bean:" + e.getCause().getClass() + "," + e.getCause().getMessage());
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}
	/**
	 * 删除用户	
	 */
	public void delUser()throws Exception {

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "delete from t_user where user_id='"+this.user_id+"'";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			
			throw new Exception("Exception_bean:" + e.getCause().getClass() + "," + e.getCause().getMessage());
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}
	/**
	 * 用户登录验证
	 * @return
	 */
	public boolean  check() {

		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs =null;
		boolean flag=false;
		try {
			conn = jdbcUtils.getConn();
		
			// 3:构造插入SQL
			String sql = "select * from t_user where user_id=? and pwd=?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, this.user_id);
			stmt.setString(2, this.pwd);
			// 4:执行SQL并返回结果
		     rs= stmt.executeQuery();
			 flag =rs.next();
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt,rs);
		}
		return flag;

	}
	/**
	 * 分页显示用户信息
	 * @return
	 */
	public Pager<User> list(int page,int size,UserForm userForm) throws Exception{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null;
		Pager<User> users=new Pager<User>();
		users.setPage(page);
		users.setSize(size);
		try {
		
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			int start=(page-1)*size;
			int limit=size;
			
			//查询SQL
			String selectSql="select * from t_user where 1=1";
			if(userForm!=null&&userForm.getFull_name()!=null&&!"".equals(userForm.getFull_name())){
				selectSql +=" and full_name like'%"+userForm.getFull_name()+"%'";
			}
			//统计SQL
			String countSql ="select count(*)as c from ("+selectSql+") as t1";
			//分页SQL
			 String pageSql=selectSql+" limit "+start+","+limit;
			 rs= stmt.executeQuery(countSql);
			 int total=0;
			 while(rs.next()){
				 total=rs.getInt("c");
			 }
			 users.setTotal(total);
			 //查询分页信息
			 rs= stmt.executeQuery(pageSql);
			 while(rs.next()){
			
				  String user_id = rs.getString("user_id");
				  String role_name = rs.getString("role_name");
				  String full_namr = rs.getString("full_name");
				  String pwd = rs.getString("pwd");
				  String ins_name = rs.getString("ins_name");
				  User user = new User(user_id, role_name, full_namr, pwd, ins_name);
				  users.getDataSets().add(user);
			 }
			 users.init();
		
		} catch (SQLException e) {
			throw new Exception("加载用户信息失败！");
		} finally {
			jdbcUtils.close(conn, stmt);
		}
		return users;

	}
	
	
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
	public String getRole_name() {
		return role_name;
	}
	public void setRole_name(String role_name) {
		this.role_name = role_name;
	}
	public String getFull_name() {
		return full_name;
	}
	public void setFull_name(String full_name) {
		this.full_name = full_name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getIns_name() {
		return ins_name;
	}
	public void setIns_name(String ins_name) {
		this.ins_name = ins_name;
	}
	
	//get/set鏂规硶
	

}
